
package com.hyst.tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hyst.tableInfo.ColumnInfo;
import com.hyst.tableInfo.TableModel;
import com.hyst.util.ConversionContext;
import com.hyst.util.MyConfig;

/**
 * @Description:
 * @Date：2017年6月20日
 * @author： DongY
 * @version ：1.0
 * 	  1.[2017年6月20日]CreatBy：dy
 */
@Component("tableInfoImpl")
public class TableInfoImpl implements TableInfo{

	private String sqlColumn="SHOW FULL COLUMNS FROM {0}";
	
	private Map<String, TableInfo> map=new HashMap<>();

	//private Map<String, TypeCnversion> castMap=new HashMap<>();
	@Autowired
	private MyConfig myConfig;
	private String castTypy="hump";
	@Override
	public List<String> getTableNames() throws SQLException {
		Connection connection=DbConnction.getConnection();
		List<String> list=new ArrayList<>();
		
		Statement statement=connection.createStatement();
		ResultSet set=statement.executeQuery("show tables");
		
		while(set.next()){
			String tableName=set.getString(1);
			map.put(tableName, null);
			list.add(tableName);
		}
		set.close();
		statement.close();
		connection.close();
		return list;
	}

	@Override
	public TableModel getTableInfoByName( String tableName) throws SQLException {
		Connection connection=DbConnction.getConnection();
		Statement statement =connection.createStatement();
		System.out.println(MessageFormat.format(sqlColumn, tableName));
		ResultSet result= statement.executeQuery(MessageFormat.format(sqlColumn, tableName));
		TypeCnversion castTool=ConversionContext.geTypeCnversion(myConfig.getNameConversion());//castMap.get(castTypy);
		TableModel tableModel=new TableModel();
		tableModel.setTableName(tableName);
		tableModel.setModelName(castTool.cast(StringUtil.headToUpper(tableName)));
		List<ColumnInfo> columnInfos=new ArrayList<>();
		while(result.next()){
			ColumnInfo columnInfo=new ColumnInfo();
			columnInfo.setColumnName(result.getString(1));
			columnInfo.setFiledName(castTool.cast(result.getString(1)));
			String jdbcType=result.getString(2);
			if(jdbcType.indexOf("(")>0){
				
				jdbcType.substring(0,jdbcType.indexOf('('));
			}
			
			columnInfo.setJdbcType(jdbcType);
			columnInfo.setPre(result.getString(5)!=null&&"PRE".equals(result.getString(5)));
			String javaType=castTool.toJavaType(jdbcType);
			if(javaType.replaceAll("\\.", "").length()<javaType.length()){
				tableModel.addImports(javaType.substring(0, javaType.lastIndexOf('.')));
				columnInfo.setJavaType(javaType.substring(javaType.lastIndexOf('.')));
			}else {
				columnInfo.setJavaType(javaType);
			}
			columnInfo.setDefaultVal(result.getObject(6));
			columnInfo.setNotNull("YES".equals(result.getString(4)));
			columnInfo.setComment(result.getString(9));
			columnInfos.add(columnInfo);
		}
		tableModel.setFiedls(columnInfos);
		return tableModel;
	}

	public static void main(String[] args) {
		System.out.println("==============");
		System.out.println("1.25".lastIndexOf('.'));
	}
	
	
}
